package com.ruoyi.business.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.ViolationCategory;
import com.ruoyi.business.dto.EditEnterpriseDTO;
import com.ruoyi.business.mapper.ViolationCategoryMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.EnterpriseMapper;
import com.ruoyi.business.domain.Enterprise;
import com.ruoyi.business.service.IEnterpriseService;
import org.springframework.util.ObjectUtils;

/**
 * 企业Service业务层处理
 * 
 * @author lcz
 * @date 2025-01-19
 */
@Service
public class EnterpriseServiceImpl implements IEnterpriseService 
{
    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private ViolationCategoryMapper violationCategoryMapper;

    /**
     * 查询企业
     * 
     * @param id 企业主键
     * @return 企业
     */
    @Override
    public Enterprise selectEnterpriseById(Long id)
    {
        return enterpriseMapper.selectEnterpriseById(id);
    }

    /**
     * 查询企业列表
     * 
     * @param enterprise 企业
     * @return 企业
     */
    @Override
    public List<Enterprise> selectEnterpriseList(Enterprise enterprise)
    {
        return enterpriseMapper.selectEnterpriseList(enterprise);
    }

    /**
     * 新增企业
     * 
     * @param enterprise 企业
     * @return 结果
     */
    @Override
    public int insertEnterprise(Enterprise enterprise)
    {
        if (!ObjectUtils.isEmpty(enterprise.getViolationCategoryId())){
            ViolationCategory violationCategory = violationCategoryMapper.selectViolationCategoryById(enterprise.getViolationCategoryId());
            enterprise.setCategoryName(violationCategory.getName());
        }

        enterprise.setCreateTime(DateUtils.getNowDate());
        return enterpriseMapper.insertEnterprise(enterprise);
    }

    /**
     * 修改企业
     * 
     * @param enterprise 企业
     * @return 结果
     */
    @Override
    public int updateEnterprise(Enterprise enterprise)
    {
        if (!ObjectUtils.isEmpty(enterprise.getViolationCategoryId())){
            ViolationCategory violationCategory = violationCategoryMapper.selectViolationCategoryById(enterprise.getViolationCategoryId());
            enterprise.setCategoryName(violationCategory.getName());
        }
        enterprise.setUpdateTime(DateUtils.getNowDate());
        return enterpriseMapper.updateEnterprise(enterprise);
    }

    /**
     * 批量删除企业
     * 
     * @param ids 需要删除的企业主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseByIds(Long[] ids)
    {
        return enterpriseMapper.deleteEnterpriseByIds(ids);
    }

    /**
     * 删除企业信息
     * 
     * @param id 企业主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseById(Long id)
    {
        return enterpriseMapper.deleteEnterpriseById(id);
    }

    @Override
    public AjaxResult frontEdit(EditEnterpriseDTO dto) {
        Enterprise enterprise = enterpriseMapper.selectEnterpriseById(dto.getId());
        if (ObjectUtils.isEmpty(enterprise)) {
            return AjaxResult.error("企业不存在");
        }

        List<Enterprise> enterprises = enterpriseMapper.selectEnterpriseListByEnterpriseName(dto.getEnterpriseName());
        if (enterprises.size()>0){
            Enterprise item = enterprises.get(0);
            if (!ObjectUtils.isEmpty(item.getOpenId())){
                return AjaxResult.error("该企业已经注册，不可重复注册");
            }else {
                enterprise.setIsViolation(item.getIsViolation());
                enterprise.setCategoryName(item.getCategoryName());
                enterprise.setViolationCategoryId(item.getViolationCategoryId());
                enterprise.setCreateBy(item.getCreateBy());
                enterprise.setCreateTime(item.getCreateTime());

                enterpriseMapper.deleteEnterpriseById(item.getId());
            }
        }

        if (!ObjectUtils.isEmpty(enterprise.getViolationCategoryId())){
            ViolationCategory violationCategory = violationCategoryMapper.selectViolationCategoryById(enterprise.getViolationCategoryId());
            enterprise.setCategoryName(violationCategory.getName());
        }


        enterprise.setUpdateTime(new Date());
        enterprise.setEnterpriseName(dto.getEnterpriseName());
        enterprise.setEnterpriseCode(dto.getEnterpriseCode());
        enterprise.setContactName(dto.getContactName());
        enterprise.setContactPhone(dto.getContactPhone());
        enterprise.setAvatar(dto.getAvatar());

        enterpriseMapper.updateEnterprise(enterprise);

        return AjaxResult.success();
    }
}
